package com.szrt.oa.employee.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szrt.oa.base.service.impl.BaseServiceImpl;
import com.szrt.oa.department.dao.DepartmentDao;
import com.szrt.oa.department.entity.DepartmentEntity;
import com.szrt.oa.dto.PageDTO;
import com.szrt.oa.employee.dao.EmployeeDao;
import com.szrt.oa.employee.entity.EmployeeEntity;
import com.szrt.oa.employee.service.EmployeeService;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<EmployeeEntity> implements EmployeeService {

	@Autowired
	private EmployeeDao dao;
	
	@Autowired
	private DepartmentDao departDao;
	
	@Override
	public List<EmployeeEntity> myfindAll() {
		
		List<EmployeeEntity> myfindAll=null;
		try {
			myfindAll = dao.myfindAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return myfindAll;
	}

	@Override
	public Map<String,Object> myfindpage(int pageIndex,int pageSize) {
		Map<String,Object> map=new HashMap<String,Object>();
		PageDTO page=new PageDTO();
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		try {
			List<EmployeeEntity> listByPage = dao.myfindPage(page);
			int acount=dao.selectAll().size();
			//如果不能整除，页数加一，否则等于页数除以分页的数
			int yeshu=acount % pageSize==0?acount/pageSize:acount/pageSize+1;
			 
			map.put("code", 200);
			map.put("pageIndex", pageIndex);
			map.put("yeshu", yeshu);
			map.put("list", listByPage);
		} catch (Exception e) {
			map.put("code", 999);
			e.printStackTrace();
		}
		return map;
		
	}
	
	@Override
	public Map<String, Object> update(EmployeeEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String departmentName = entity.getDepartmentName();
			DepartmentEntity deEntity = departDao.fingIDByName(departmentName);
			Integer id = deEntity.getId();
			entity.setDeparmentId(id);
			
			int count=dao.updateByPrimaryKeySelective(entity);
			map.put("code", 200);
			map.put("count", count);
		} catch (Exception e) {
			map.put("code", 999);
			e.printStackTrace();
		}
		return map;
	}
	
	
	@Override
	public Map<String, Object> queryById(EmployeeEntity entity) {
	
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			EmployeeEntity queryById = dao.queryById(entity);
			map.put("code", 200);
			map.put("entity", queryById);
		} catch (Exception e) {
			map.put("code", 999);
			e.printStackTrace();
		}
		return map;
	
	}
	
}

	
	

